﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace WDCF
{
    /// <summary>
    /// Converts strings to boolean values
    /// </summary>
    public class StringToBoolConverter : IValueConverter
    {
        /// <summary>
        /// Converts a string value to a boolean value. The compare value and a value of
        /// the <see cref="StringOperator"/> enumeration is extracted from the parameter
        /// object.
        /// </summary>
        /// <returns>
        /// A converted value according to the given operator value. 
        /// If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <exception cref="ArgumentException">If the parameters is not a number and <see cref="StringOperator.Length"/> is used.</exception>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string v = value.ToString();

            string para = parameter.ToString();
            //operator and compare value are separated by |
            string[] split = para.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

            StringOperator stringOperator;
            //try to parse out the operator
            if (Enum.TryParse(split[0], true, out stringOperator))
            {
                string p = split[1];
                switch (stringOperator)
                {
                    case StringOperator.Equals:
                        return v.Equals(p);
                    case StringOperator.Contains:
                        return v.Contains(p);
                    case StringOperator.StartsWith:
                        return v.StartsWith(p);
                    case StringOperator.EndsWith:
                        return v.EndsWith(p);
                    case StringOperator.Length:
                        int length;
                        if(int.TryParse(p, out length))
                        {
                            return v.Length == length;
                        }
                        throw new ArgumentException(
                            string.Format(
                                "The given compare value ({0}) cannot be used to compare the length of the string.",
                                split[1]));
                }
            }
            return false;
        }

        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("Converting values back is currently not supported.");
        }
    }
}
